package cn.bdqn.service.impl;
import cn.bdqn.domain.Cargo;
import cn.bdqn.domain.Order;
import cn.bdqn.domain.OrderForm;
import cn.bdqn.mapper.CargoMapper;
import cn.bdqn.mapper.OrderFormMapper;
import cn.bdqn.mapper.OrderMapper;
import cn.bdqn.service.OrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author dkj
 */
@Service
public class OrderFormServiceImpl implements OrderFormService {


    @Autowired
    private OrderFormMapper orderFormMapper;


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CargoMapper cargoMapper;


    /**
     * 新增
     * @param orderForm
     * @return
     */
    @Override
    public OrderForm saveOrderForm(OrderForm orderForm) {

        //根据id查询订单
        Order order = orderMapper.selectByPrimaryKey(orderForm.getOrderId());
        //赋值
        orderForm.setOrder(order);
        //查询货物
        Cargo cargo = cargoMapper.selectByPrimaryKey(orderForm.getCargoId());
        //设置货物对象
        orderForm.setCargo(cargo);
        //设置货物名
        orderForm.setCargoName(cargo.getCargoName());
        //设置重量
        orderForm.setCargoSumWeight(orderForm.getCargoSumNumber()*cargo.getCargoWeight());
        //设置总体积
        orderForm.setCargoSumBulk(cargo.getCargoBulk()*orderForm.getCargoSumNumber());
        //添加
        orderFormMapper.insertOrderForm(orderForm);
        //返回结果
        return orderForm;
    }

    /**
     * 根据订单id查询
     * @param orderId
     * @return
     */
    @Override
    public List<OrderForm> queryByOrderId(Integer orderId) {
        return orderFormMapper.selectByOrderId(orderId);
    }

    /**
     * 更新订单货物
     * @param orderForm
     */
    @Override
    public OrderForm updateOrderFrom(OrderForm orderForm) {


        //根据id查询订单对象
        Order order = orderMapper.selectByPrimaryKey(orderForm.getOrderId());
        //设置订单对象
        orderForm.setOrder(order);
        //查询货物对象
        Cargo cargo = cargoMapper.selectByPrimaryKey(orderForm.getCargoId());
        //设置货物对象
        orderForm.setCargo(cargo);
        //设置货物名
        orderForm.setCargoName(cargo.getCargoName());
        //设置重量
        orderForm.setCargoSumWeight(orderForm.getCargoSumNumber()*cargo.getCargoWeight());
        //设置总体积
        orderForm.setCargoSumBulk(cargo.getCargoBulk()*orderForm.getCargoSumNumber());
        //更新订单货物
        orderFormMapper.updateOrderFrom(orderForm);
        //查询更新后的数据
        OrderForm orderForm1 = orderFormMapper.selectByPrimaryKey(orderForm.getId());
        //返回
        return orderForm1;
    }
}
